package freezzz.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import freezzz.client.TestService;
import freezzz.client.EventWidget.TimelineEvent;
import freezzz.server.EventDriver.DriverFriendFeedEvents;
import freezzz.server.EventDriver.EventsDriver;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;


public class TestServiceImpl extends RemoteServiceServlet implements TestService {
    private static Hashtable<Date, ArrayList<TimelineEvent>> eventsCash = new Hashtable<Date, ArrayList<TimelineEvent>> ();
    private boolean ALLOWFUTURE = false;
    private EventsDriver eventDriver;

    public TestServiceImpl () {
        eventDriver = new DriverFriendFeedEvents ();
        // eventDriver = new DriverTwitterEvents ();
       /// eventDriver = new DriverDummyEvents ();
        Calendar toDate = Calendar.getInstance ();
        Calendar sinceDate = Calendar.getInstance ();

        sinceDate.add ( Calendar.MONTH, -1 );
        toDate.add ( Calendar.DATE, 1 );

        populate ( sinceDate, toDate );
    }


    public void populate ( Calendar sinceDate, Calendar toDate ) {
        eventsCash.clear ();
        Calendar tmpdate = getDayDate ( sinceDate );

        while ( tmpdate.before ( toDate ) ) {
            eventsCash.put ( tmpdate.getTime (), eventDriver.getEventsForDate ( tmpdate.getTime () ) );
            tmpdate.add ( Calendar.DATE, 1 );
        }
    }


    public TimelineEvent[] getEventData () {
        return null;
    }


    public TimelineEvent[] getEventForDate ( Date date ) {
        if ( !ALLOWFUTURE && date.after ( new Date () ) ) {
            System.out.println ( "No future allowed" );
            return null;
        }

        date = getDayDate ( date );

        if ( !eventsCash.containsKey ( date ) ) {
            eventsCash.put ( date, eventDriver.getEventsForDate ( date ) );
        }
        System.out.println ( "Get date events:" + date + " Event num:" + eventsCash.get ( date ).size () );
        return eventsCash.get ( date ).toArray ( new TimelineEvent[0] );
    }

    public static Calendar getDayDate ( Calendar date ) {
        Calendar tmpdate = Calendar.getInstance ();
        tmpdate.clear ();
        tmpdate.set ( Calendar.YEAR, date.get ( Calendar.YEAR ) );
        tmpdate.set ( Calendar.MONTH, date.get ( Calendar.MONTH ) );
        tmpdate.set ( Calendar.DATE, date.get ( Calendar.DATE ) );

        return tmpdate;
    }

    public static Date getDayDate ( Date date ) {
        Calendar tmpdate = Calendar.getInstance ();
        tmpdate.setTime ( date );
        Calendar result = Calendar.getInstance ();
        result.clear ();
        result.set ( Calendar.YEAR, tmpdate.get ( Calendar.YEAR ) );
        result.set ( Calendar.MONTH, tmpdate.get ( Calendar.MONTH ) );
        result.set ( Calendar.DATE, tmpdate.get ( Calendar.DATE ) );

        return result.getTime ();
    }
}